<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class TagHasNewsDB  extends DBReader{

    //检查信息是否已经添加
    public function getTagHasNews($tag_id,$new_id){

        $sql = "select tag_id,new_id from tag_has_news where tag_id = :tag_id and new_id = :new_id";

        $bindarr = array(
            'tag_id'=>$tag_id,
            'new_id'=>$new_id,
        );
        return $this->db()->getAll($sql,$bindarr);
    }

    //添加 tag 拥有的 new 信息
    public function addTagHasNews($tag_id,$new_id){

        $sql = "insert into tag_has_news(tag_id,new_id) values(:tag_id,:new_id)";

        $bindarr = array(
            'tag_id'=>$tag_id,
            'new_id'=>$new_id,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除 tag_id 对应的信息
    public function delByTagId($tag_id){
        $bindarr = array(
            'tag_id'=>$tag_id,
        );

        $sql = "delete from tag_has_news where tag_id = :tag_id";
        $this->db()->execute($sql,$bindarr);
    }

    //删除 new_id 对应的信息
    public function delByNewId($new_id){
        $bindarr = array(
            'new_id'=>$new_id,
        );

        $sql = "delete from tag_has_news where new_id = :new_id";
        $this->db()->execute($sql,$bindarr);
    }

    //删除 new_id and tag_id 对应的信息
    public function delByNewIDAndTagID($new_id,$tag_id){
        $bindarr = array(
            'new_id'=>$new_id,
            'tag_id'=>$tag_id
        );

        $sql = "delete from tag_has_news where new_id = :new_id and tag_id = :tag_id";
        $this->db()->execute($sql,$bindarr);
    }
}